CC = gcc
PP = gcc
AR = ar rcv
ifeq ($(windir),)
EXE =
RM = rm -f
RMDIR = rm -rf
else
EXE = .exe
RM = del
RMDIR = rmdir /Q /S
endif

CFLAGS = -O2 -Os -fno-exceptions -fvisibility=hidden -ffunction-sections -fdata-sections -Wextra -fno-exceptions
LDFLAGS = -Wl,--gc-sections,--print-gc-sections
INC = -I.

UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Darwin)
    LDFLAGS += -dead_strip
else
    LDFLAGS +=
endif
SUBDIRS = bzip2 BSDIFF BSPATCH

bziplib = bzip2/bzlib.o bzip2/crctable.o bzip2/compress.o bzip2/decompress.o\
	bzip2/randtable.o bzip2/blocksort.o bzip2/huffman.o
all:sub bindir bin/Bspatch$(EXE)  bin/Bsdiff$(EXE)
sub:
	for dir in $(SUBDIRS); do \
	cd $$dir && make && cd ../; \
	done

bindir:
	mkdir -p bin

bin/Bsdiff$(EXE):BSDIFF/bsdiff.o $(bziplib)
	$(CROSS_COMPILE)$(PP) -o $@ $^ $(LDFLAGS) -s

bin/Bspatch$(EXE):BSPATCH/bspatch.o
	$(CROSS_COMPILE)$(PP) -o $@ $^ $(LDFLAGS) -s


clean:
	-$(RM) $(all)
